package com.horoscope.astrology.zodiac.signs.free.fragment

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import com.jeremyliao.liveeventbus.LiveEventBus
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.adapter.HoroscopeAdapter
import com.horoscope.astrology.zodiac.signs.free.bean.ContentBean
import com.horoscope.astrology.zodiac.signs.free.bean.DataResultBean
import com.horoscope.astrology.zodiac.signs.free.utils.SubFragmentDecoration

class SubFragment : BaseFragment() {
    private lateinit var adapter: HoroscopeAdapter
    private var position = 0
    private var title = ""

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_sub, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        title = arguments?.getString("titleOfList") ?: ""
        position = arguments?.getInt("position") ?: 0
        adapter = HoroscopeAdapter(activity)
        val mRecyclerView = view.findViewById<RecyclerView>(R.id.recycler_view_article)
        if (title.isEmpty()) {
            return
        }
        if (setData()) return

        mRecyclerView.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(activity)
        mRecyclerView.addItemDecoration(SubFragmentDecoration(15f, 20f))
        adapter.setPosition(position)
        adapter.setTitleType(title)
        mRecyclerView.adapter = adapter
    }

    private fun setData(): Boolean {
        LiveEventBus.get("subFragment", DataResultBean::class.java)
            .observeSticky(this, Observer { t ->

                val list = ArrayList<ContentBean>()
                when (title) {
                    "Today" -> {
                        val bean = ContentBean()
                        bean.ad = true
                        t.data.today.add(1, bean)
                        adapter.setData(t.data.today)
                    }
                    "Tomorrow" -> {
                        val bean1 = ContentBean()
                        bean1.ad = true
                        t.data.tomorrow.add(1, bean1)
                        adapter.setData(t.data.tomorrow)
                    }
                    "Month" -> {
                        val bean = ContentBean()
                        bean.content = t.data.month
                        list.clear()
                        list.add(bean)
                        adapter.setData(list)
                    }
                    "Year" -> {
                        val bean = ContentBean()
                        bean.content = t.data.year
                        list.clear()
                        list.add(bean)
                        adapter.setData(list)
                    }
                    "2020" -> {
                        val bean = ContentBean()
                        bean.content = t.data.newyear2020
                        list.clear()
                        list.add(bean)
                        adapter.setData(list)
                    }
                }
            })
        return false

    }

    override fun onResume() {
        super.onResume()
        val filter = IntentFilter()
        filter.addAction("update.signIcon")
        activity.registerReceiver(mBroadcastReceiver, filter)

    }

    private val mBroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (setData()) return
        }
    }

    override fun onPause() {
        super.onPause()
        activity.unregisterReceiver(mBroadcastReceiver)

    }
}